<?php
	/*	用户类 */
class User extends MySql{
	/*检查登陆
	$u_name 邮箱地址
	$u_pwd  用户密码
	返回错误提示或true*/
	function checkUserLogin($u_name,$u_pwd){
		if($this->validateEmail($u_name)){
			$data = $this->query($this->select('*','speed_user','user_email',$u_name),'one');
			$pwd=md5($u_pwd);
			if($pwd==$data['user_pwd']){
				$_SESSION['login_mail']=$u_name;
				$_SESSION['login_pwd']=$pwd;
				return true;
			}			
		}
		return false;
	}
	
	//检查是否登陆
	function checkLogin(){
		if(isset($_SESSION['login_mail'])){
			$data = $this->query($this->select('*','speed_user','user_email',$_SESSION['login_mail']),'one');
			if($data['user_pwd'] == $_SESSION['login_pwd'])return true;
		}else return false;
	}
	
	//检查邮箱是否存在
	function checkExits($mail){
		$data = $this->query($this->select('user_mail','speed_user','user_mail',$mail),'num');
		if($data == 0)return false;
		return true;
	}
	
	//检查用户更必密码时原始密码是否正确
	function checkUserPwd($pwd){
		if($this->checkLogin()){
			$uin = $this->query($this->select('user_pwd','speed_user','user_email',$_SESSION['login_mail']),'one');
			if(md5($pwd) == $uin['user_pwd'])return true;
			else return false;
		}return false;
	}
	
	//更新用户信息
	function updateUserPwd($pwd){
		if($this->checkLogin()){
			if($this->query($this->update('speed_user',array("user_pwd" => md5($pwd)),'user_email',$_SESSION['login_mail']),'none')){
				$_SESSION['login_pwd'] = md5($pwd);
				return true;
			}
		}return false;
	}
	
	//返回当前登陆用户的信息
	function getUserId($ac = ''){
		if($this->checkLogin()){
			$data = $this->query($this->select('u_id,user_nick,user_email','speed_user','user_email',$_SESSION['login_mail']),'one');
			if($ac != '')return $data[$ac];
			else return $data;
		}else return false;
	}
				
	/*$user 邮箱地址
	$href 跳转地址
	return void*/

	function login($user,$pwd,$href){
		$_SESSION['login_mail']=$user;
		$_SESSION['login_pwd']=$pwd;
		echo "<script language=\"javascript\">location.href='$href'</script>";
	}
	
	function loginOut(){
		session_destroy();
		return true;
	}

	function checkPwd($pwd){
		if (!ereg("^[_a-zA-Z0-9]*$",$pwd)) {
			return false;
		}
		return true;
	}
	
	function checkMail($mail){
		if($this->validateEmail($mail))return true;
		return false;
	}
	
	function addNewUser($val){
		$status = $this->query($this->insert('speed_user',$val),'none');
		//$user = $this->checkEmail($val['user_email']);
		//$status = mysql_query("insert into speed_user (user_email,u_id,create_time,user_pwd) values('$user','$val[u_id]','$val[create_time]','$val[user_pwd]')")or die(mysql_error());
		return $status;
	}
	
	function checkEmail($email){
		$str=preg_replace("/\+|\*|\`|\/|\-|\$|\#|\^|\!|\%|\&|\~|\^|\[|\]|\'|\"|\<|\>|\=|\;/", "", $email);
		return $str;
	}
	
	function addSession($user,$pwd,$keep=0,$ac=0){
		$data = array("user_email" => $user,
							  "user_pwd" => $pwd,
							  "create_time" => date("Y-m-d H:i:s",time()),
							  "keep_login" => $keep,
							  "action" => $ac);
		$status = $this->query($this->insert('speed_session',$data),'none');
		return $status;
	}
	
	
	/*$email 邮箱地址
	return true or false
	标准邮箱地址检测
	*/
	
	function validateEmail($email){
		$isValid = true;
		$atIndex = strrpos($email,"@");
		if (is_bool($atIndex) && !$atIndex){
			$isValid = false;
		}
		else {
			$domain = substr($email, $atIndex+1);
			$local = substr($email, 0, $atIndex);
			$localLen = strlen($local);
			$domainLen = strlen($domain);
			if ($localLen < 1 || $localLen > 64){
    	     // local part length exceeded
				$isValid = false;
				}
			else if ($domainLen < 1 || $domainLen > 255){
    	     // domain part length exceeded
				$isValid = false;
			}
			else if ($local[0] == '.' || $local[$localLen-1] == '.')
			{
			   // local part starts or ends with '.'
			   $isValid = false;
			}
			else if (preg_match('/\\.\\./', $local))
			{
			   // local part has two consecutive dots
			   $isValid = false;
			}
			else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
			{
			   // character not valid in domain part
			   $isValid = false;
			}
			else if (preg_match('/\\.\\./', $domain))
			{
			   // domain part has two consecutive dots
			   $isValid = false;
			}
			else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
			           str_replace("\\\\","",$local)))
			{
			   // character not valid in local part unless 
			   // local part is quoted
			   if (!preg_match('/^"(\\\\"|[^"])+"$/',
			       str_replace("\\\\","",$local)))
			   {
			      $isValid = false;
			   }
			}
			if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
			{
			   // domain not found in DNS
			   $isValid = false;
			}
   }
   return $isValid;
	}
	

}
?>